/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.lin.utils;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

/**
 *
 * @author sony
 */
public class XmlUtils {
    
    private static String fileName =    "users.xml";
    
    public static Document getDocument() throws DocumentException{
        
        URL url = XmlUtils.class.getClassLoader().getResource(fileName);
        //String realPath = url.getPath();
        SAXReader reader = new SAXReader();
        Document document = reader.read(url);
        return document;        
    }
    
    public static void write2Xml(Document document) throws UnsupportedEncodingException, IOException {
        
        URL url = XmlUtils.class.getClassLoader().getResource(fileName);
        String realPath = url.getPath();
        // Pretty print the document to System.out
        OutputFormat format = OutputFormat.createPrettyPrint();
        XMLWriter writer = new XMLWriter( new FileOutputStream(realPath), format );
        writer.write( document );
        writer.close();
    }
}
